package hdd.config;

import hdd.MassageDrivenBean.UserMDB;
import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitAdmin;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.rabbit.listener.MessageListenerContainer;
import org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer;
import org.springframework.amqp.rabbit.listener.adapter.MessageListenerAdapter;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.EnvironmentAware;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;

@Configuration
@PropertySource({"classpath:setting.properties"})
public class AMQPConfig implements EnvironmentAware {
    private Environment environment;

    @Bean
    public ConnectionFactory connectionFactory() {
        CachingConnectionFactory connectionFactory = new CachingConnectionFactory();
        connectionFactory.setHost( environment.getProperty( "AMQP.host" ) );
        connectionFactory.setPort( environment.getProperty( "AMQP.port", Integer.class ) );
        String username = environment.getProperty( "AMQP.username" );
        String password = environment.getProperty( "AMQP.password" );
        if (username != null && !"".equals( username ) && password != null && !"".equals( password )) {
            connectionFactory.setUsername( username );
            connectionFactory.setUsername(password);
        }
        return connectionFactory;
    }

    @Bean
    public Queue queue() {
        Queue queue = new Queue( "test.amqp.queue" );
        return queue;
    }

    @Bean
    public DirectExchange directExchange(ConnectionFactory connectionFactory) {
        DirectExchange directExchange = new DirectExchange( "test.amqp.directExchange" );
        return directExchange;
    }

    @Bean
    public RabbitAdmin rabbitAdmin(ConnectionFactory connectionFactory, DirectExchange directExchange, Queue queue) {
        RabbitAdmin rabbitAdmin = new RabbitAdmin( connectionFactory );
        rabbitAdmin.declareExchange( directExchange );
        rabbitAdmin.declareQueue( queue );
        Binding binding = BindingBuilder.bind( queue ).to( directExchange ).with( "test.amqp.routingKey" );
        rabbitAdmin.declareBinding( binding );
        return rabbitAdmin;
    }


    @Bean
    public AmqpTemplate amqpTemplate(ConnectionFactory connectionFactory) {
        AmqpTemplate amqpTemplate = new RabbitTemplate( connectionFactory );
        return amqpTemplate;
    }

    @Bean
    public UserMDB userMDB() {
        return new UserMDB();
    }

    //    配置 消息监听
    @Bean
    @Qualifier("MessageListener")  //amqpTemplate 也实现了 MessageListener 接口，为避免冲突而加入qualifier
    public MessageListener channelAwareMessageListener(UserMDB userMDB) {
        MessageListenerAdapter messageListener = new MessageListenerAdapter();
        messageListener.setDelegate( userMDB );
        messageListener.setDefaultListenerMethod( "receiveUser" );
        return messageListener;
    }

    //配置 消息监听器容器
    @Bean
    public MessageListenerContainer messageListenerContainer(@Qualifier("MessageListener") MessageListener messageListener, ConnectionFactory connectionFactory) {
        SimpleMessageListenerContainer messageListenerContainer = new SimpleMessageListenerContainer();
        messageListenerContainer.setupMessageListener( messageListener );
        messageListenerContainer.setAutoDeclare( true );
        messageListenerContainer.setConnectionFactory( connectionFactory );
        messageListenerContainer.addQueueNames( "test.amqp.queue" );
        return messageListenerContainer;
    }


    @Override
    public void setEnvironment(Environment environment) {
        this.environment = environment;
    }
}
